<?php

namespace App\Admin\Eloquent;

use DB;

class Message extends Model
{

    /**
     * 系统消息类型,对应code_type
     */
    const MESSAGE_TYPE = 'message_type';
    /**
     * 接收人类型数据字典
     */
    const RECEIVER_TYPE = 'receiver_type';
    /**
     * 实例化模型可批量赋值
     *
     * @var array
     */
    protected $fillable = ['id', 'title', 'content', 'announcer', 'receiver_type', 'type', 'published_at'];

    /**
     * 查询系统消息类型
     */
    public static function get_message_types()
    {
        return Code::getByTypeId(self::MESSAGE_TYPE);
    }

    /**
     * 查询接收人类型
     */
    public static function get_receiver_types()
    {
        $codes = [
            new Code(['code' => 'all_users', 'type_id' => self::RECEIVER_TYPE, 'name' => '全部接收人']),
            new Code(['code' => 'select_users', 'type_id' => self::RECEIVER_TYPE, 'name' => '指定接收人']),
        ];

        return $codes;
    }

    /**
     * 返回所有接收人用户集合
     *
     * @return mixed
     */
    public static function get_receivers()
    {
        //实例化users
        $users['0'] = new User(['name' => 'root', 'id' => 1]);
        $users['1'] = new User(['name' => '唐僧', 'id' => 2]);
        $users['2'] = new User(['text' => '白骨精', 'id' => 3]);
        $users['3'] = new User(['text' => '太子妃', 'id' => 4]);
        $users['4'] = new User(['text' => '葫芦娃', 'id' => 5]);
        $users['5'] = new User(['text' => '小笼包', 'id' => 6]);

        return $users;
    }

    public function users()
    {
        return $this->belongsToMany('App\User', 'user_message', 'user_id', 'message_id');
    }

    /**
     * 根据消息title模糊查询消息
     *
     * @param array $args
     * @param int $pagesize
     */
    public static function paging_query(array $args, int $pagesize=15)
    {
        $query = Message::query();

        if (!empty($args['title']))
            $query = $query->where('title', 'like', '%' . $args['title'] . '%');

        return $messages = $query->orderBy('created_at', 'desc')->paginate($pagesize);
    }

    /**
     * 发送一封站内信给指定的用户
     * 
     * @param  string $messageType [消息类型]
     * @param  string $title [标题]
     * @param  string $content     [正文]
     * @param  int    $user_id     [用户id]
     * @return [type]              [状态]
     */
    public static function sendMessage(string $messageType,string $title, string $content, int $user_id,string $author = 'system'){
        try {
            $args = [
                'title' => $title,
                'type' => $messageType,
                'announcer' => $author,
                'published_at' => date('Y-m-d H:i:s'),
                'announcer' => 'select_users',
                'content' => $content
            ];

            //保存消息
            $messages = new Message($args);
            $messages->save();

            //获取消息id
            $msg_id = $messages->id;

            //保存消息接受人关系
            DB::select("insert INTO user_messages SELECT $msg_id,id,0   from users WHERE id = $user_id");

            //保存消息接受人关系

            return true;
        } catch (Exception $e) {
            return false;
        }
    }
}

